<template>
  <div class="main-card">
    <el-card class="box-card">
      <template #header>
        <span>调度日志</span>
      </template>
      <el-form ref="ruleForm" :model="queryParams" label-width="100px">
        <div class="search-form">
          <el-form-item label="任务名称">
            <el-input v-model="queryParams.jobName" clearable />
          </el-form-item>
          <el-form-item label="任务组名">
            <el-input v-model="queryParams.jobGroup" clearable />
          </el-form-item>
          <el-form-item label="执行状态">
            <el-select v-model="queryParams.status" clearable placeholder="请选择">
              <el-option
                label="正常"
                :value="1"
              />
              <el-option
                label="暂停"
                :value="0"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="dateRange" label="执行时间">
            <el-date-picker
              v-model="dateRange"
              clearable
              type="daterange"
              format="YYYY-MM-DD"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-button style="margin-left: 20px" type="primary" @click="search">搜索</el-button>
        </div>
      </el-form>
      <div style="margin:20px 0;">
        <el-button v-hasPermi="['monitor:job:remove']" type="danger" icon="el-icon-delete"  @click="deleteLog">删除</el-button>
        <el-button v-hasPermi="['monitor:job:remove']" type="danger" icon="el-icon-delete"  @click="clean">清空</el-button>
      </div>
      <div>
        <el-table
          :data="dataList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column
          prop="jobLogId"
          label="日志编号"
          width="90"
        />
        <el-table-column
          prop="jobName"
          label="任务名称"
          width="120"
        />
        <el-table-column
          prop="jobGroup"
          label="任务组名"
          width="120"
        />
        <el-table-column
          prop="invokeTarget"
          label="调度目标字符串"
          width="200"
        />
        <el-table-column
          prop="jobMessage"
          label="日志信息"
          width="300"
        />
        <el-table-column
          prop="status"
          label="执行状态"
          width="120"
        >
          <template #default="{row}">
            {{row.status ? '正常': '失败'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="执行时间"
          width="140"
          :formatter="timeFormat"
        />
        <el-table-column
          prop="msg"
          label="操作"
          width="200"
        >
         <template #default="{row}">
            <el-button v-hasPermi="['monitor:job:query']" type="primary" icon="el-icon-caret-right" @click="detail(row)">详细</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        v-show="total>0"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
      />
      </div>
    </el-card>
    <task-log-detail :value="isDetailShow" @close="isDetailShow = false"/>
  </div>
</template>

<script lang="ts">
import { toRefs, reactive, watchEffect, defineComponent } from 'vue'
import { useStore } from 'vuex'
import { ElMessageBox, ElMessage } from 'element-plus'
import moment from 'moment'
import TaskLogDetail from './components/TaskLogDetail.vue'
import { JobLog, StateType as LogStateType } from '@/store/modules/monitor/taskLog'
import { StateType as LogininfoStateType } from '@/store/modules/system/logininfo'
import tableList, { BaseQueryType, BaseDataType } from '@/mixins/tableList'

interface QueryParamsType extends BaseQueryType{
  jobName: string;
  jobGroup: string;
  status: number | null;
  beginTime: string | null;
  endTime: string | null;
}

interface DataType extends BaseDataType<JobLog>{
  isDetailShow: boolean;
  queryParams: QueryParamsType;
  dateRange: Array<string>;
}

export default defineComponent({
  components: { TaskLogDetail },
  name: 'TaskLog',
  setup() {
    const store = useStore<{taskLog: LogStateType; logininfo: LogininfoStateType}>()
    const data = reactive<DataType>({
      url: 'taskLog/getList',
      isDetailShow: false,
      dateRange: [],
      dataList: [],
      selectList: [],
      total: 0,
      queryParams: {
        jobName: '',
        jobGroup: '',
        status: null,
        beginTime: null,
        endTime: null,
        pageNum: 1,
        pageSize: 10
      }
    })
    const {
      getList,
      handleSelectionChange,
      handleCurrentChange,
      search
    } = tableList(data)
    search.value()
    watchEffect(() => {
      if (data.dateRange && data.dateRange.length >= 2) {
        data.queryParams.beginTime = moment(data.dateRange[0]).format('YYYY-MM-DD')
        data.queryParams.endTime = moment(data.dateRange[1]).format('YYYY-MM-DD')
      } else {
        data.queryParams.beginTime = null
        data.queryParams.endTime = null
      }
    })

    const deleteLog = () => {
      const ids = (data.selectList || []).map(item => item.jobLogId)
      if (ids.length === 0) {
        return
      }
      ElMessageBox.confirm('您确定要删除"' + ids + '"日志吗, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return store.dispatch('logininfo/deleteLog', ids)
      }).then(() => {
        ElMessage({
          message: '删除成功',
          type: 'success'
        })
        getList.value()
      }).catch()
    }
    const clean = () => {
      ElMessageBox.confirm('您确定要清空日志吗, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return store.dispatch('logininfo/clean')
      }).then(() => {
        ElMessage({
          message: '清空成功',
          type: 'success'
        })
        getList.value()
      }).catch()
    }
    const detail = (row: JobLog) => {
      store.commit('taskLog/SET_DATA', row)
      data.isDetailShow = true
    }
    return {
      handleSelectionChange,
      handleCurrentChange,
      search,
      ...toRefs(data),
      deleteLog,
      clean,
      detail
    }
  }
})
</script>

<style lang="scss">
.el-pagination{
  margin-top: 20px;
}
</style>
